/*let UserNameAll;
getUserNameAll()*/
let us_name = "a";
let urlStr = getParamVal();
let article_id = urlStr.article_id
//运行详情页面
get_details(urlStr.article_id)
//获取评论并且写入
setComInput()
getComment()
//添加文章点击
add_hits()

/**
 * 每次进来加一次点击量
 */
function add_hits() {
    future.ajax({
        url: '/article/addHist',
        type: 'post',
        data: {
            article_id
        },
        success: function (result) {
            console.log(result)
        }
    })

}


/**
 * 获取评论
 */
function getComment() {
    future.ajax({
        url: '/article/getComment',
        type: 'post',
        data: {
            article_id
        },
        success: function (result) {
            console.log(result)
            add_Comment(result)
        }
    })

}


/**
 * 获取用户名与id对应表
 */
function getUserNameAll() {
    future.ajax({
        url: '/users/getUserNameAll',
        type: 'post',
        success: function (result) {
            UserNameAll = result
        }
    })
}

/**
 * 渲染评论
 * @param result
 */
function add_Comment(result) {
    let com_put = ``
    result.forEach((item, index) => {
        getUsernameChangeName(item.u_id)
        item.time = rTime(item.time)
        com_put += `
         <li>
                <span>` + us_name + `:</span>
                <p>` + item.content + `</p>
                <i>` + item.time + `</i>
            </li>
        `
    })
    document.querySelector('#comment_main_ul').innerHTML = com_put
    window.scrollTo(0, document.body.scrollHeight)


}


/**
 * 绑定发表评论功能
 */
function setComInput() {
    document.querySelector('#comment_btn').onclick = function () {
        let content = document.querySelector('#comment_input1').value;
        setComment(content, article_id)
    }
}

/**
 * 写入评论
 * @param content
 * @param article_id
 */
function setComment(content, article_id) {
    future.ajax({
        url: '/article/setComment',
        type: 'post',
        data: {
            content,
            article_id
        },
        success: function (result) {
            console.log(result)
            alert("发表评论成功")
            getComment()
        }
    })

}


/**
 * 详情页面
 * @param art_id 文章id
 */
function get_details(article_id) {
    future.ajax({
        url: '/article/getDetails',
        type: 'get',
        data: {
            article_id,
        },
        success: function (result) {
            // let getUsername=getUsername();
            add_details(result[0])
            getUsername(result[0].u_id)
        }
    })
}


function getUsername(u_id) {
    future.ajax({
        url: '/users/getUserName',
        type: 'post',
        data: {
            u_id,
        },
        success: function (result) {
            document.querySelector('#deta_name').innerText = result
        }
    })
}

function getUsernameChangeName(u_id) {
    future.ajax({
        url: '/users/getUserName',
        type: 'post',
        data: {
            u_id,
        },
        success: function (result) {
            us_name = result
            // document.querySelector('#deta_name').innerText=result
        }
    })
}

function add_details(result) {
    let add_deta = ``;
    console.log("dasdasdasdasd")
    console.log(result.time)
    result.time = rTime(result.time)
    document.querySelector('#details_top').innerHTML = `
                <h1 id="details_mytitle">` + result.title + `</h1>
                <div class="details_top_titles">
                    <span id="deta_name">洪吉林</span>
                    <span>` + result.time + `</span>
                    <span>` + result.hits + `点击量</span>
                </div>
    `
    document.querySelector('#details_centent_main_text').innerHTML = result.content

}


/**
 * 截取跳转页面带的参数
 */
function getParamVal() {
    let params = {};

    if (location.search !== '') {
        let arr = location.search.substring(1).split('&');

        for (let i = 0, iLen = arr.length; i < iLen; i++) {
            let aTmp = arr[i].split('='), value = decodeURIComponent(aTmp[1]), numberVal = Number(value);
            //处理数字
            if (typeof numberVal === 'number' && numberVal === numberVal) {
                value = numberVal;
            }
            //处理布尔值
            if (value === 'true' || value === 'false') {
                value = value === 'true';
            }
            params[aTmp[0]] = value;
        }
    }
    return params;
}


/**
 * 转换时间格式
 * @param date
 * @returns {string}
 */
function rTime(date) {
    var json_date = new Date(date).toJSON();
    return new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
}
